package com.littleHan.brushQuestions.web.userCenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.*;
import com.littleHan.brushQuestions.utils.StringUtil;
import com.littleHan.brushQuestions.web.start.dao.UserDao;
import com.littleHan.brushQuestions.web.start.pojo.*;
import com.littleHan.brushQuestions.web.userCenter.dao.AdminUserCenterDao;
import com.littleHan.brushQuestions.web.userCenter.pojo.Announcement;
import com.littleHan.brushQuestions.web.userCenter.pojo.DicType;
import com.littleHan.brushQuestions.web.userCenter.pojo.Pagination;
import com.littleHan.brushQuestions.web.userCenter.pojo.vo.GeneralUserRoleVo;
import com.littleHan.brushQuestions.web.userCenter.pojo.vo.SearchVo;
import com.littleHan.brushQuestions.web.userCenter.service.AdminUserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminUserCenterServiceImpl implements AdminUserCenterService {

    @Autowired
    private AdminUserCenterDao adminUserCenterDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Map<String, Object> getGeneralUserList(Pagination pagination) {
        //获取普通用户信息
        List<GeneralUser> generalUserList =adminUserCenterDao.getGeneralUserList(pagination.getPageNo(),pagination.getPageSize());
        //获取普通用户的总数量
        int total=adminUserCenterDao.getGeneralUserNum();
        Map<String,Object> map=new HashMap<>();
        map.put("generalUserList",generalUserList);
        map.put("total",total);
        return map;
    }

    @Override
    @Transactional
    public void banAndLift(GeneralUser generalUser) throws FailedToUpdateException{
        int result=adminUserCenterDao.updateGeneralUserViolation(generalUser.getId(),generalUser.getViolation());
        if(result!=1){
            throw new FailedToUpdateException("修改封禁状态失败");
        }
    }

    @Override
    public List<Role> getRoleList() {
        List<Role> roleList=adminUserCenterDao.getRoleList();
        return roleList;
    }

    @Override
    public List<String> getGeneralUserRole(String id) throws FailedToLoadException {
        List<String> roleIdList=adminUserCenterDao.getGeneralUserRole(id);
        if(roleIdList.isEmpty()){
            throw new FailedToLoadException("获取当前普通用户已有权限失败");
        }
        return roleIdList;
    }

    @Override
    @Transactional(rollbackFor = {FailedSettingException.class})
    public void updateGeneralUserRole(GeneralUserRoleVo generalUserRoleVo) throws FailedSettingException{
        //获取当前普通用户的用户id
        User user=userDao.getUserByGeneralUserId(generalUserRoleVo.getGeneralUserId());
        //删除该普通用户的所有权限
        int deleteResult=adminUserCenterDao.deleteUserRoleByUserId(user.getId());
        if(deleteResult<1){
            throw new FailedToDeleteException("修改用户权限失败");
        }
        //插入权限
        for(String s:generalUserRoleVo.getRoleList()){
            UserRole userRole=new UserRole();
            userRole.setId(StringUtil.generateUUID());
            userRole.setUserId(user.getId());
            userRole.setRoleId(s);
            int insertResult=adminUserCenterDao.insertUserRole(userRole);
            if(insertResult<1){
                throw new FailedToInsertException("修改用户权限失败");
            }
        }
    }

    @Override
    public Map<String, Object> getAuditorList(Pagination pagination) {
        //获取拥有审核员权限的普通用户信息
        List<GeneralUser> auditorList=adminUserCenterDao.getAuditorList(pagination.getPageNo(),pagination.getPageSize());
        //获取拥有审核员权限的普通用户总数量
        int total=adminUserCenterDao.getAuditorNum();
        Map<String,Object> map=new HashMap<>();
        map.put("auditorList",auditorList);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> searchGeneralUser(SearchVo searchVo) {
        //根据条件获取搜索结果
        List<GeneralUser> searchList=adminUserCenterDao.searchGeneralUser(searchVo);
        //根据条件获取搜索结果总数量
        int total=adminUserCenterDao.searchGeneralUserNum(searchVo);
        Map<String,Object> map=new HashMap<>();
        map.put("searchList",searchList);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> getDicType(Pagination pagination) throws FailedToLoadException {
        List<DicValue> dicValueList=adminUserCenterDao.getDicValue(pagination.getPageNo(),pagination.getPageSize(),"type");
        if(dicValueList.isEmpty()){
            throw new FailedToLoadException("获取类型失败");
        }
        int total=adminUserCenterDao.getDicValueNum("type");
        Map<String,Object> map=new HashMap<>();
        map.put("dicTypeList",dicValueList);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> getDicDifficult(Pagination pagination) throws FailedToLoadException {
        List<DicValue> dicDifficultList=adminUserCenterDao.getDicValue(pagination.getPageNo(),pagination.getPageSize(),"difficult");
        if(dicDifficultList.isEmpty()){
            throw new FailedToLoadException("获取难度等级失败");
        }
        int total=adminUserCenterDao.getDicValueNum("difficult");
        Map<String,Object> map=new HashMap<>();
        map.put("dicDifficultList",dicDifficultList);
        map.put("total",total);
        return map;
    }

    @Override
    public Map<String, Object> getDicObject(Pagination pagination) throws FailedToLoadException {
        List<DicValue> dicObjectList=adminUserCenterDao.getDicValue(pagination.getPageNo(),pagination.getPageSize(),"object");
        if(dicObjectList.isEmpty()){
            throw new FailedToLoadException("获取科目失败");
        }
        int total=adminUserCenterDao.getDicValueNum("object");
        Map<String,Object> map=new HashMap<>();
        map.put("dicObjectList",dicObjectList);
        map.put("total",total);
        return map;
    }

    @Override
    @Transactional(rollbackFor = {FailedToUpdateException.class})
    public void updateDicValue(DicValue dicValue) throws FailedToUpdateException{
        int result=adminUserCenterDao.updateDicValue(dicValue);
        if(result!=1){
            throw new FailedToUpdateException("修改数据字典值失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {FailedToDeleteException.class})
    public String deleteDicValue(String id) throws FailedToDeleteException{
        int selectResult=adminUserCenterDao.selectIsHaveDicValue(id);
        if(selectResult>0){
            return JSON.toJSONString(new Result().setCode(500).setMessage("该数据字典不能删除，原因：“已有题目绑定该数据字典”"));
        }
        else{
            int result=adminUserCenterDao.deleteDicValue(id);
            if(result!=1){
                throw new FailedToDeleteException("删除数据字典时发生错误");
            }
            return JSON.toJSONString(new Result().setCode(200).setMessage("数据字典删除成功"));
        }
    }

    @Override
    public List<DicType> getDicTypeList() {
        List<DicType> list=adminUserCenterDao.getDicTypeList();
        return list;
    }

    @Override
    @Transactional(rollbackFor = {FailedToInsertException.class})
    public void addDicValue(DicValue dicValue) throws FailedToInsertException{
        dicValue.setId(StringUtil.generateUUID());
        int result=adminUserCenterDao.addDicValue(dicValue);
        if(result!=1){
            throw new FailedToInsertException("新建数据字典失败");
        }
    }

    @Override
    public Map<String, Object> getAnnouncementList(Pagination pagination) {
        List<Announcement> announcementList=adminUserCenterDao.getAnnouncementList(pagination.getPageNo(),pagination.getPageSize());
        int total=adminUserCenterDao.getAnnouncementListNum();
        Map<String,Object> map=new HashMap<>();
        map.put("announcementList",announcementList);
        map.put("total",total);
        return map;
    }

    @Override
    @Transactional(rollbackFor = {FailedToDeleteException.class})
    public void deleteAnnouncement(Announcement announcement) throws FailedToDeleteException {
        int deleteResult=adminUserCenterDao.deleteAnnouncement(announcement.getId());
        if(deleteResult!=1){
            throw new FailedToDeleteException("删除公告失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {FailedToInsertException.class})
    public void addAnnouncement(Announcement announcement) throws FailedToInsertException {
        announcement.setId(StringUtil.generateUUID());
        announcement.setTime(StringUtil.getSysTime());
        int result=adminUserCenterDao.addAnnouncement(announcement);
        if(result!=1){
            throw new FailedToInsertException();
        }
    }
}
